# Actinic-PC项目全局配置文件
import yaml
# 打开yaml文件
fs = open("resources/config/config.yaml", encoding="UTF-8")
datas = yaml.load(fs, Loader=yaml.FullLoader)

# region 配置

class camera:
    cameraConfig = datas['camera']
    url = cameraConfig['url']
    screenShotPath = cameraConfig['screenShotPath']
    fps = cameraConfig['fps']

class serial:
    serialConfig = datas['serial']
    port = serialConfig['port']
    bps = serialConfig['bps']
    timeout = serialConfig['timeout']
    readType = serialConfig['readType']
    readLen = serialConfig['readLen']
    readLine = serialConfig['readLine']

class server:
    serverConfig = datas['server']
    ip = serverConfig['ip']
    port = serverConfig['port']
    websocketUrl = f'ws://{ip}:{port}/webSocket/'

# endregion

def read():
    with open('resources/config/config.yaml', encoding="utf-8") as f:
        datas = yaml.load(f, Loader=yaml.FullLoader)
        parse(datas)

def write(datas):
    with open('resources/config/config.yaml', 'w', encoding="utf-8") as f:
        yaml.dump(datas, f)
        parse(datas)

def save():
    datas = toJson()
    with open('resources/config/config.yaml', 'w', encoding="utf-8") as f:
        yaml.dump(datas, f)

def toJson():
    datas = {}
    cameraConfig = {}
    cameraConfig['url'] = camera.url
    cameraConfig['screenShotPath'] = camera.screenShotPath
    cameraConfig['fps'] = camera.fps

    serialConfig = {}
    serialConfig['port'] = serial.port
    serialConfig['bps'] = serial.bps
    serialConfig['timeout'] = serial.timeout
    serialConfig['readType'] = serial.readType
    serialConfig['readLen'] = serial.readLen
    serialConfig['readLine'] = serial.readLine

    serverConfig = {}
    serverConfig['ip'] = server.ip
    serverConfig['port'] = server.port

    datas['camera'] = cameraConfig
    datas['serial'] = serialConfig
    datas['server'] = serverConfig

    return datas

def parse(datas):
    cameraConfig = datas['camera']
    camera.url = cameraConfig['url']
    camera.screenShotPath = cameraConfig['screenShotPath']
    camera.fps = cameraConfig['fps']

    serialConfig = datas['serial']
    serial.port = serialConfig['port']
    serial.bps = serialConfig['bps']
    serial.timeout = serialConfig['timeout']
    serial.readType = serialConfig['readType']
    serial.readLen = serialConfig['readLen']
    serial.readLine = serialConfig['readLine']

    serverConfig = datas['server']
    server.ip = serverConfig['ip']
    server.port = serverConfig['port']


if __name__ == '__main__':
    print(datas)
